package com.ddw.meedeeuh.tests.shared.movie.scrapers.imdb;

import static org.junit.Assert.*;

import java.io.File;
import java.util.regex.Pattern;

import org.junit.Test;

import com.ddw.meedeeuh.shared.io.SourceFile;
import com.ddw.meedeeuh.shared.movie.MovieSettings;
import com.ddw.meedeeuh.shared.movie.scrapers.SearchMovieScraper;
import com.ddw.meedeeuh.shared.movie.scrapers.SearchMovieScraperResult;
import com.ddw.meedeeuh.shared.movie.scrapers.MovieScraperResultType;
import com.ddw.meedeeuh.shared.movie.scrapers.imdb.ImdbSearchMovieScraperFactory;

public class ImdbMovieScraperTest {

	@Test
	public void testScrape_success() {
		SourceFile file;
		String nameRegex, yearRegex;
		boolean matchNameOnParentFolder, matchYearOnParentFolder;
		MovieSettings settings;
		SearchMovieScraper scraper = ImdbSearchMovieScraperFactory.create();
		SearchMovieScraperResult result;

		// Test 'charlie and the chocolate factory' very specific
		file = new SourceFileImpl("c:\\movies\\Charlie And The Chocolate Factory (2005)\\Charlie And The Chocolate Factory (2005).bluray.mkv");
		nameRegex = "([^\\(]+)";
		yearRegex = "([0-9]{4})";
		matchNameOnParentFolder = true;
		matchYearOnParentFolder = true;
		settings = new MovieSettingsImpl(nameRegex, yearRegex, matchNameOnParentFolder, matchYearOnParentFolder);
		result = scraper.scrape(settings, file);
		assertEquals(MovieScraperResultType.SUCCESS, result.getResult());
		assertEquals(1, result.getMovies().size());
		assertEquals("Charlie and the Chocolate Factory", result.getMovies().get(0).getName());
		assertEquals(2005, result.getMovies().get(0).getYear());

		// Test 'alien' has more than one film, but year is specific
		file = new SourceFileImpl("c:\\movies\\Alien (1979)\\Alien (1979).dvd.mkv");
		nameRegex = "([^\\(]+)";
		yearRegex = "([0-9]{4})";
		matchNameOnParentFolder = true;
		matchYearOnParentFolder = true;
		settings = new MovieSettingsImpl(nameRegex, yearRegex, matchNameOnParentFolder, matchYearOnParentFolder);
		result = scraper.scrape(settings, file);
		assertEquals(MovieScraperResultType.SUCCESS, result.getResult());
		assertEquals(1, result.getMovies().size());
		assertEquals("Alien", result.getMovies().get(0).getName());
		assertEquals(1979, result.getMovies().get(0).getYear());

		// Test 'stay' this has multiple results 'stay' and 'stay with me'
		file = new SourceFileImpl("c:\\movies\\Stay (2005)\\Stay (2005).hddvd.avi");
		nameRegex = "([^\\(]+)";
		yearRegex = "([0-9]{4})";
		matchNameOnParentFolder = true;
		matchYearOnParentFolder = true;
		settings = new MovieSettingsImpl(nameRegex, yearRegex, matchNameOnParentFolder, matchYearOnParentFolder);
		result = scraper.scrape(settings, file);
		assertEquals(MovieScraperResultType.SUCCESS, result.getResult());
		assertEquals(2, result.getMovies().size());
		assertEquals("Stay", result.getMovies().get(0).getName());
		assertEquals(2005, result.getMovies().get(0).getYear());
		assertEquals("Stay with Me", result.getMovies().get(1).getName());
		assertEquals(2005, result.getMovies().get(1).getYear());
	}

	private static class SourceFileImpl implements SourceFile {

		private final File file;

		public SourceFileImpl(String filePath) {
			this.file = new File(filePath);
		}

		@Override
		public File getInstance() {
			return this.file;
		}

	}

	private static class MovieSettingsImpl implements MovieSettings {

		private final Pattern nameRegex;
		private final Pattern yearRegex;
		private final boolean matchNameOnParentFolder;
		private final boolean matchYearOnParentFolder;

		public MovieSettingsImpl(String nameRegex, String yearRegex,
				boolean matchNameOnParentFolder, boolean matchYearOnParentFolder) {
			this.nameRegex = Pattern.compile(nameRegex);
			this.yearRegex = Pattern.compile(yearRegex);
			this.matchNameOnParentFolder = matchNameOnParentFolder;
			this.matchYearOnParentFolder = matchYearOnParentFolder;
		}

		@Override
		public Pattern getNameRegex() {
			return this.nameRegex;
		}

		@Override
		public Pattern getYearRegex() {
			return this.yearRegex;
		}

		@Override
		public boolean matchNameOnParentFolder() {
			return this.matchNameOnParentFolder;
		}

		@Override
		public boolean matchYearOnParentFolder() {
			return this.matchYearOnParentFolder;
		}

		@Override
		public String getTmdbApiKey() {
			return null;
		}

		@Override
		public String getLibraryPath() {
			return null;
		}

		@Override
		public String[] getMovieExtensions() {
			return null;
		}

		@Override
		public boolean parentFolderNameMustMatchMovie() {
			return false;
		}

		@Override
		public String[] getIgnoreStrings() {
			return null;
		}

		@Override
		public boolean matchNameOnNfo() {
			return false;
		}

		@Override
		public boolean usePosters() {
			return false;
		}

		@Override
		public boolean useFanart() {
			return false;
		}

		@Override
		public boolean useExtraThumbs() {
			return false;
		}

		@Override
		public String getPosterName() {
			return null;
		}

		@Override
		public String getFanartName() {
			return null;
		}

		@Override
		public String getExtraThumbsFolderName() {
			return null;
		}

		@Override
		public Pattern getExtraThumbsPattern() {
			return null;
		}

		@Override
		public String getExtraThumbsFormat() {
			return null;
		}

		@Override
		public MovieNfoConstants getNfoConstants() {
			return null;
		}

		@Override
		public MovieExportNodes getExportNodes() {
			return null;
		}

		@Override
		public boolean getPrettyFormatNfo() {
			return false;
		}

		@Override
		public void setPrettyFormatNfo(boolean prettyFormat) { }

		@Override
		public boolean getAddEmptyNfoNodes() {
			return false;
		}

		@Override
		public void setAddEmptyNfoNodes(boolean addEmptyNfoNodes) { }

	}

}
